<div class="container">
  <div class="bg-putih">
    <?php $filter = $this->session->userdata('filter');?>
    <div class="jdl-page">
      <?php echo $judul ?>
      <div class="clearfix"></div>
    </div>

    <div class="row">
      <div class="col-md-3">
        <div class="btn-group btn-group-sm" role="group">
          <a href="<?php echo site_url('transaksi_pembelian/add') ?>" class="btn btn-primary"><i class="fa fa-plus"></i> Tambah</a>
          <a href="<?php echo site_url('transaksi_pembelian/excel') ?>" class="btn btn-warning"><i class="fa fa-file-excel-o"></i> Unduh Excel</a>
        </div>
      </div>      
      <div class="col-md-7 pull-right">
        <?php echo form_open('transaksi_pembelian/search'); ?>
        <div class="col-sm-6" id="changethis">
          <div class="row">
            <div class="col-sm-6" style="padding:0px;">
              <input id="waktu_awal" type="text" name="waktu_awal" class="form-control input-sm" placeholder="Waktu awal"/>
            </div>
            <div class="col-sm-6">
              <input id="waktu_akhir" type="text" name="waktu_akhir" class="form-control input-sm" placeholder="Waktu akhir"/>
            </div>
          </div>          
        </div>
        <div class="col-sm-6" style="padding:0px;">
          <select name="filter" class="form-control input-sm" style="width:80%; display:inline-block" onchange='get_filter(this.value)'>
            <option value="by_waktu">Waktu</option>
            <option value="by_petugas">Petugas</option>
            <option value="by_kelompok">Kelompok</option>
            <option value="by_obat">Obat</option>
          </select>
          <button type="submit" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-search"></span></button>
        </div>
        <?php echo form_close(); ?>
        <script type="text/javascript">
          $("#waktu_awal, #waktu_akhir").datepicker({
            dateFormat: "yy-mm-dd",
            changeMonth: true,
            changeYear: true
          });

          function get_filter(filter){                  
            var html_code;
            if(filter == "by_waktu"){
              html_code = '<div class="row"> <div class="col-sm-6" style="padding:0px;"> <input id="waktu_awal" type="text" name="waktu_awal" class="form-control input-sm" placeholder="Waktu awal"/> </div> <div class="col-sm-6"> <input id="waktu_akhir" type="text" name="waktu_akhir" class="form-control input-sm" placeholder="Waktu akhir"/> </div> </div> ';
              $('#changethis').html(html_code);
              $(document).ready(function(){
                $('#waktu_awal, #waktu_akhir').datepicker({
                  dateFormat: "yy-mm-dd",
                  changeMonth: true,
                  changeYear: true
                });
              });
            }else if(filter == "by_petugas"){              
              html_code = '<?php $query = $this->db->get("rc_users")->result_array(); ?> <select id="petugas" class="form-control input-sm" name="petugas"> <?php foreach($query as $rows): ?> <option value="<?php echo $rows['id'] ?>"><?php echo $rows['username'] ?></option> <?php endforeach; ?> </select> <script type="text/javascript"> $("#petugas").select2();';
              $('#changethis').html(html_code);
            }else if(filter == "by_kelompok"){
              html_code = '<?php $query = $this->db->get("kelompok")->result_array(); ?> <select class="form-control input-sm" name="kelompok"> <?php foreach($query as $rows): ?> <option value="<?php echo $rows['KdKelompok'] ?>"><?php echo $rows['NmKelompok'] ?></option> <?php endforeach; ?> </select> <script type="text/javascript"> $("#petugas").select2();';
              $('#changethis').html(html_code); 
            }else if(filter == "by_obat"){
              html_code = '<?php $query = $this->db->get("obat")->result_array(); ?> <select id="petugas" class="form-control input-sm" name="obat"> <?php foreach($query as $rows): ?> <option value="<?php echo $rows['KdObat'] ?>"><?php echo $rows['NmObat'] ?></option> <?php endforeach; ?> </select> <script type="text/javascript"> $("#petugas").select2();';
              $('#changethis').html(html_code); 
            }
          }
        </script>
      </div>
      
    </div>
    <table class="table table-striped table-hover margintop20">
      <thead>
        <tr>
          <th>Kode</th>
          <th>Tanggal</th>
          <th>Keterangan</th>
          <th>User</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach($results as $row): ?>
          <tr>
            <td width="15%"><?php echo $row['KdBeli'] ?></td>
            <td width="20%"><?php echo $row['Tanggal'] ?></td>
            <td width="35%"><?php echo $row['Keterangan'] ?></td>
            <td width="15%"><?php echo $row['nickname'] ?></td>
            <td width="15%" align="center">
              <a href="<?php echo site_url("transaksi_pembelian/edit/$row[KdBeli]") ?>" class="btn btn-xs btn-info">Edit</a>
              <a href="<?php echo site_url('transaksi_pembelian/delete/' . $row['KdBeli']) ?>" class="btn btn-xs btn-danger" title="Hapus" onClick="return confirm('Yakin ingin menghapus data transaksi ini?')">Hapus</a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if(count($results) == 0): ?>
          <tr>
            <td colspan="5" align="center">Tidak ada data pembelian</td>
          </tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>